package nc.bs.pub.action;

import java.util.Arrays;

import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.IWorkFlowRet;
import nc.itf.hrppl.pjapplication.pjappl.IPJApplDefService;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.VOStatus;
import nc.vo.uap.pf.PFBusinessException;
import nccloud.commons.lang.StringUtils;
import nc.codeplatform.framework.service.AbstractPfScriptAction;
import nc.vo.hrppl.pjapplication.AggPjapplVO;
import nc.itf.hrppl.pjapplication.pjappl.IPJApplHVOService;

public class N_PL01_APPROVE extends AbstractPfScriptAction<AggPjapplVO> {

	public N_PL01_APPROVE() {
		super();
	}

	@Override
	protected Object process(PfParameterVO paraVO) throws BusinessException {
		try {
			IPJApplHVOService service = NCLocator.getInstance().lookup(IPJApplHVOService.class);
			AggPjapplVO[] vos = (AggPjapplVO[]) this.getVos();
			Arrays.stream(vos).forEach(v -> v.getParent().setStatus(VOStatus.UPDATED));

			IWorkFlowRet workFlowRet = (IWorkFlowRet) this.procActionFlow(paraVO);

			vos = service.callbackAPPROVE(vos);
			// modify by wy at 2025年6月18日 按照业务归口科室实现重要性排序
			NCLocator.getInstance().lookup(IPJApplDefService.class).pjApplApprove(vos);
			return workFlowRet == null ? vos : workFlowRet;
		} catch (Exception ex) {
			if (ex instanceof BusinessException) {
				throw (BusinessException) ex;
			}
			throw new PFBusinessException(ex.getMessage(), ex);
		}
	}

	protected void before(PfParameterVO paraVO) throws BusinessException {
		AggPjapplVO vo = (AggPjapplVO) this.getVo();
		if (StringUtils.isEmpty(vo.getPrimaryKey())) {
			throw new BusinessException("当前单据未执行保存动作脚本，如果配置了业务流程定义，请检查动作驱动配置！");
		}
	}
}